home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / C++ Toolbox 1.0 / Collections++.cp next >
Text File  |  1995-11-16  |  2KB  |  104 lines

  1. /*
  2.     File:        CustomAttributes.cp
  3.     
  4.     Contains:    Quickie minimal implementation of
  5.                 Collection Manager for System 7.x.
  6.     
  7.     Version:    1.0 (for System 7.5)
  8.     
  9.     Copyright:    ©1995 Chris K. Thomas.  All Rights Reserved.
  10. */
  11.  
  12. #include "Collections++.h"
  13.  
  14. //
  15. // add a tag if nonexistant or change existing tag
  16. //
  17.  
  18. void
  19. TagTable::SetTag(FlavorType inTag, void * inData, long inDataSize)
  20. {
  21.     Ptr tagPtr;
  22.  
  23.     if(!LookupTag(inTag, &tagPtr))
  24.     {
  25.         //
  26.         // add tag
  27.         //
  28.         CollectableTag    ourTag;
  29.         
  30.         ourTag.tag = inTag;
  31.         ourTag.ptr = NewPtrClear(inDataSize);
  32.         
  33.         Assert_(ourTag.ptr);
  34.         
  35.         if(ourTag.ptr == NULL)
  36.             throw MemError();
  37.         
  38.         BlockMoveData(inData, ourTag.ptr, inDataSize);
  39.         
  40.     //    DebugNum(mTagTable.getcount() + 1);
  41.         mTagTable.setcount(mTagTable.getcount() + 1);
  42.         
  43.         mTagTable.lock();
  44.         mTagTable[mTagTable.getcount() - 1] = ourTag;
  45.         mTagTable.unlock();
  46.     }
  47.     else
  48.     {
  49.         //
  50.         // set tag
  51.         //
  52.         Assert_(tagPtr);
  53.         SetPtrSize(tagPtr, inDataSize);
  54.         
  55.         long error = MemError();
  56.         
  57.         if(error != noErr)
  58.             throw error;
  59.         
  60.         BlockMoveData(inData, tagPtr, inDataSize);
  61.     }
  62. }
  63.  
  64. TagTable::~TagTable()
  65. {
  66.     mTagTable.lock();
  67.     
  68.     long    tagCount = mTagTable.getcount();
  69.     
  70.     for(long curTag = 0; curTag < tagCount; curTag++)
  71.     {
  72.         Assert_(mTagTable[curTag].ptr);
  73.         DisposePtr(mTagTable[curTag].ptr);
  74.     }
  75.     
  76.     mTagTable.unlock();
  77. }
  78.  
  79. //
  80. // LookupTag
  81. // Given a tag, return the corresponding data
  82. //
  83. Boolean
  84. TagTable::LookupTag(FlavorType inTag, Ptr *outData)
  85. {
  86.     mTagTable.lock();
  87.  
  88.     long    tagCount = mTagTable.getcount();
  89.     Boolean outFound = false;
  90.  
  91.     for(long curTag = 0; curTag < tagCount; curTag++)
  92.     {
  93.         if(mTagTable[curTag].tag == inTag)
  94.         {
  95.             *outData = mTagTable[curTag].ptr;
  96.             outFound = true;
  97.             break;
  98.         }
  99.     }
  100.     
  101.     mTagTable.unlock();
  102.     
  103.     return outFound;
  104. }